<template>
  <div class="card-page">
    <tool-bar nav="" :title="action_name + '语言资源'">
      <el-button size="small" @click="$router.push('/system/lang-resource/add')" v-if="$props.action=='edit'">新建</el-button>
      <el-button size="small" @click="save_click" type="primary">保存</el-button>
    </tool-bar>
    <el-row :gutter="12" type="flex">
      <el-col :span="12">
        <el-card shadow="always">
            <template #header> <span>基本信息</span> </template>
          <kv label="productLine">
            <selector v-model.trim="info.productLine.code" readOnly
                      url="/dev/product-line/list" fields="code,name"
                      @change="line_change"
                      chk="*"/>
          </kv>
          <kv label="key">
            <el-input v-model="info.key"/>
          </kv>
          <kv label="remark">
            <el-input v-model="info.remark" type="textarea"/>
          </kv>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="always">
            <template #header> <span>语言</span> </template>
          <div>中文:</div>
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 8 }" v-model="info.cn"/>
          <div style="margin-top:20px;">英语</div>
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 8 }" v-model="info.en"/>

        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
/**
 * Created by CodeGenerator  at 2022-05-27 17:28:52
 */






//定义 vue2的this,当前组件
const $self = getCurrentInstance();

//定义 $attrs
const $attrs = useAttrs();



//定义data
const $data = reactive({"info":{"productLine":{}}});
const info = $computed(()=> $data.info );

const $props = defineProps({
    action: { type: String } ,
    id : { type: String }
});


const list = ref();

//定义事件
const $emit = defineEmits(["update:modelValue","loaded"]);

const action = $computed( function() {
    return $props.id ? "edit" : "add";
});
const action_name = $computed( function() {
    return {add: "添加", edit: "修改"}[$props.action]
});

const line_change = function(v, model) {
    $data.info.productLine = {id: model.id, code: model.code, name: model.name}
};
const loadData = async function() {
    if (!$props.id) return;
    var res = await jv.$http.post("/system/lang-resource/detail/" + $props.id)
    $data.info = res.data.data;
};
const save_click = async function() {
//校验
    if (jv.chk_main($data) == false) {
        return;
    }
    var res = await jv.$http.post("/system/lang-resource/save", $data.info)
//[axios拦截器中已处理了异常]。
    jv.info(action_name + " 成功");
    if ($props.action == "add") {
        var id = res.data.data
        jv.setLastRowId("/system/lang-resource/list", "list", id);
        jv.$router.push("/system/lang-resource/edit/" + id)
    } else if ($props.action == "edit") {
        jv.$router.push("/system/lang-resource/list")
    }
};

onMounted( function() {
    $data.info.productLine.code = jv.$route.query.productLineCode;
    loadData();
} )



</script>
